/*
 * Copyright (C) 2013 'Chaos'.Personal
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.silly.adapter.entity;

import java.util.ArrayList;
import java.util.List;

import android.util.SparseArray;

/**
 * 视图，数据对应关系.(每一个条目)
 */
public class EntityItem {

    private int mLayoutId = 0;
    private SparseArray<List<Object>> mMapping = null;
    
    private EntityItem(EntityItemBuilder builder){
    	mLayoutId = builder.layoutId;
    	mMapping  = builder.mapping;
    }
	
    /**
     * 获取Item的LayoutId
     */
    public int getLayoutId(){
    	return mLayoutId;
    }
    
    public int size(){
    	return mMapping.size();
    }
    
    public int getViewId(int index){
    	return mMapping.keyAt(index);
    }
    
    public List<Object> getData(int viewId){
    	return mMapping.get(viewId);
    }
    
	public static class EntityItemBuilder{
		private int layoutId;
		private SparseArray<List<Object>> mapping = new SparseArray<List<Object>>();
		
		public EntityItemBuilder put(int layoutId){
			this.layoutId = layoutId;
			return this;
		}
		
		public EntityItemBuilder put(int viewId,Object obj){
            //check list
		    List<Object> oldValue = mapping.get(viewId);
		    if(oldValue == null){
		    	oldValue = new ArrayList<Object>();
		    	mapping.put(viewId, oldValue);
		    }
		    oldValue.add(obj);
			return this;
		}
        
		public EntityItem build(){
			return new EntityItem(this);
		}
	}
}
